const listBehavior = require("../../../behaviors/list");
const util = require("../../../utils/util");
const network = require("../../../utils/network");
const app = getApp()
Component({
    behaviors: [listBehavior],
    options: {
        multipleSlots: true // 在组件定义时的选项中启用多 slot 支持
    },
    /**
     * 组件的属性列表
     */
    properties: {
        leftWidth: {
            type: Number,
            value: 0
        },
        rightWidth: {
            type: Number,
            value: 0
        },
        hasLeft: {
            type: Boolean,
            value: false
        },
        hasLeft1: {
            type: Boolean,
            value: false
        },
        hasLeft2: {
            type: Boolean,
            value: false
        },
        hasRight: {
            type: Boolean,
            value: false
        },
        hasRight1: {
            type: Boolean,
            value: false
        },
        hasRight2: {
            type: Boolean,
            value: false
        },
        lazy: {
            type: Boolean,
            value: false
        },
        showAddFollowBtn: {
            type: Boolean,
            value: false
        }
    },
    ready() {
        if (this.data.lazy) {
            return
        }
        this.getList()
    },
    data: {
        disableShow: true// 禁用behaviors中的show生命周期
    },
    methods: {
        getList(isAdd = false) {
            const _this = this
            return new Promise((resolve, reject) => {
                util.showLoading('加载中')
                network.GET({
                    uri: _this.properties.requestUrl,
                    params: Object.assign({}, _this.data.listParams, _this.properties.requestData),
                    success(res) {
                        res.data.records.forEach(item => {
                            item.img_list = item.img_list.map(img => {
                                return {
                                    url: img,
                                    type: 'image',
                                    isImage: true
                                }
                            })
                        })
                        _this.setData({
                            'listParams.page': res.data.current,
                            'listParams.total': res.data.total,
                            'listParams.pages': res.data.pages || 1,
                            list: isAdd ? ([].concat(_this.data.list, res.data.records || [])) : (res.data.records || [])
                        })
                        if (!isAdd) wx.stopPullDownRefresh()
                        util.hideLoading()
                        resolve(res)
                    },
                    fail() {
                        if (!isAdd) wx.stopPullDownRefresh()
                        util.hideLoading()
                        reject()
                    }
                })
            })
        },
        showAddFollowDialog() {
            this.selectComponent('#addFollowDialog').showDialog();
        },
        saveFollowSuccess() {
            this.setData({list: []})
            this.getList()
        }
    },
});
